const path = require('path')
const server = require('./src/server')
const CompressionPlugin = require('compression-webpack-plugin')

const isDev = process.env.NODE_ENV === 'development'

module.exports = {
  outputDir: 'work',
  productionSourceMap: false,
  configureWebpack: config => {
    config.externals = {
      vue: 'Vue',
      'vue-router': 'VueRouter',
      'vue-i18n': 'VueI18n',
      vuex: 'Vuex',
      axios: 'axios',
      echarts: 'echarts',
      'element-ui': 'ELEMENT',
      marked: 'marked',
      qq: 'qq',
      BMap: 'BMap',
      tinymce: 'tinymce'
    }

    /** gzip */
    if (!isDev) {
      const compressionPlugin = new CompressionPlugin({
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
        threshold: 10240,
        minRatio: 0.8
      })
      config.plugins = [...config.plugins, compressionPlugin]
    }
  },
  chainWebpack: config => {
    /** svg config */
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })

    /** alias */
    config.resolve.alias.set('public', path.join(__dirname, 'public'))
  },
  devServer: {
    before(app) {
      server(app)
    }
  }
}
